package com.dragon.english_practice_back.entity;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

@Data
public class LoginUserDetails implements UserDetails, Serializable {
    User user;

    private List<String> role;

    private List<GrantedAuthority> authorities;
    public LoginUserDetails(){
        super();
    }
    public LoginUserDetails(User user){
        this.user = user;
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(Objects.isNull(authorities) || authorities.isEmpty()){
            authorities = new LinkedList<>();
            for(String r : role){
                authorities.add(new SimpleGrantedAuthority(r));
            }
        }
        return authorities;

    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getEmail();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
